package com.gy.boot.common.dao;

import java.util.ArrayList;

public class IPage<E> extends ArrayList<E> {
    private static final long serialVersionUID = -3395905259007380208L;

    /** 页码, 从1开始 */
    private int pageNo;
    /** 每页数据,范围: 1 - MAX_PAGE_SIZE */
    private int pageSize;
    /** 起始行 */
    private int start;
    /** 末行 */
    private int end;
    /** 总条数 */
    private int total;
    /** 总页数 */
    private int pages;
    /** 是否查询 count, 默认 true 查询 */
    private boolean count;
    /** 查询Count的Mapper中SqlID */
    private String countSqlID;

    public IPage() { super(); }
    public IPage(int pageNo, int pageSize) {
        this(pageNo, pageSize, true);
    }
    public IPage(int pageNo, int pageSize, boolean count) {
        this(pageNo, pageSize, count, null);
    }
    public IPage(int pageNo, int pageSize, String countSqlID) {
        this(pageNo, pageSize, true, countSqlID);
    }
    public IPage(int pageNo, int pageSize, boolean count, String countSqlID) {
        super(getCorrectPageSize(pageSize));
        this.count = count;
        this.pageNo = Math.max(pageNo, 1);  // 最小为第一页
        this.pageSize = getCorrectPageSize(pageSize);
        this.countSqlID = countSqlID;
        calculateStartAndEndRow();
    }
    /** pageSize最大为1000条分页 */
    private static final int MAX_PAGE_SIZE = 1000;
    /** 计算正确的 pageSize (1 <= pageSize <= MAX_PAGE_SIZE) */
    private static int getCorrectPageSize(int pageSize) {
        return pageSize < 1 ? 1 : (pageSize > MAX_PAGE_SIZE ? MAX_PAGE_SIZE : pageSize);
    }
    /**  计算起止行 */
    private void calculateStartAndEndRow() {
        this.start = (this.pageNo - 1) * this.pageSize;
        this.end = this.start + this.pageSize * this.pageNo;
    }

/** ====================== get/set =============================== */
    public int getPageNo() { return this.pageNo; }
    public int getPageSize() { return this.pageSize; }
    public int getStart() { return this.start; }
    public int getEnd() { return this.end; }
    public int getPages() { return this.pages; }
    public boolean isCount() { return this.count; }
    public String getCountSqlID() { return this.countSqlID; }
    public void setCountSqlID(String countSqlID) { this.countSqlID = countSqlID; }
    public int getTotal() { return this.total; }
    public void setTotal(int total) {
        this.total = total;
        this.pages = (int) Math.ceil((double) total/this.pageSize);
    }

    @Override
    public String toString() {
        return "Page{count=" + count
            + ", pageNo=" + pageNo + ", pageSize=" + pageSize
            + ", start=" + start + ", end=" + end
            + ", total=" + total + ", pages=" + pages
            + '}' + super.toString();
    }

    /** 根据数据 */
    public IEntity getPageMap() {
        IEntity page = new IEntity();
        page.put("pageNo", this.pageNo);
        page.put("pageSize", this.pageSize);
        page.put("start", this.start);
        page.put("end", this.end);
        page.put("pages", this.pages);
        page.put("total", this.total);
        page.put("list", this);
        return page;
    }
}
